--公告排行榜

require "net/quitPlayerHandler"
---@class NoticeManager
NoticeManager = {
    buffId = 0,
    openTime = 0,
    endTime = 0,--七日结束时间
    lastRewardEndTime = 0,--八日结束时间
    changeAwardList = {},
    alertPlayerList = {},
    --bigRankType = 0,
    curSubRankType = 0,
    defaultSubRankTypeList = {},    --玩家所在的分榜
    rankType = {},      --玩家所在的分榜
    partRank = {},
    mainRanks = {},
    rewardRedPointList = {},    --七日榜奖励红点
    myReward = {},      --分榜每日结算奖励
    subRankUnlockRewardList = {},--分榜解锁、奖励信息
    maxNumList = {},    --榜单最大排名
    PlayerRankList={},
    selfInfo={},
    myRank=0,
    dayClick=0;
    sumClick = 0;
    FirstRnaklist={};
}
local t = NoticeManager

t.RankType = {
    arena = 101,    --竞技榜
    mythic = 102,   --秘境榜
    tower = 103,    --爬塔榜
    social = 104,   --社交榜 
    Chaos = 105,    --风暴峡湾 
    Level = 106,    -- 关卡榜(单服深渊榜)
}
t.PartBigType = {
    power = 1,  --战力大榜
    social = 2, --社交大榜
    cost = 3,   --消耗大榜
    billboard = 4,--公告榜
}
t.PartSubType = {
    all = 0,
    myth = 1,   --神话榜
    legend = 2, --传奇榜
    overlord = 3,--霸主榜
    superHero = 4,    --英雄榜
    hero = 5,     --英杰榜
}
t.NO_MAIN_CHOOSE_TYPE = 6

function t.init()
    for i = 1,3 do --大榜
        t.maxNumList[i] = {}
        for j = 1,5 do--分榜
            local c = dataTable.getDataTableByMultiId("t_noticebase",i,j)
            if j == 1 then
                t.maxNumList[i][j] = c.f_Num
            else
                t.maxNumList[i][j] = c.f_Num + t.maxNumList[i][j-1]
            end
        end
    end
end
function t.Clear()
    table.clear(t.changeAwardList)
    table.clear(t.alertPlayerList)
end
function t.GetTodayBuffCfg()
    local time = HelperFunc.GetServerTime()
    local month = os.date("%m",time)
    local day = os.date("%d",time)
    --去除0
    local timestr = tonumber(month).."-"..tonumber(day)
    if t.todayBuffCfg then
        if t.todayBuffCfg.f_Date == timestr then
            return t.todayBuffCfg
        end
    end
    t.todayBuffCfg = dataTable.getDataTableByMultiId("t_noticebuff",timestr)
    return t.todayBuffCfg
end
function t.GetMainRanksByType(rankType)
    return t.mainRanks[rankType]
end
function t.IsOpen()
    return HelperFunc.GetServerTime() >= t.openTime
end
--七日结束时间
function t.IsTimeEnd()
    return HelperFunc.GetServerTime() >= t.endTime
end
function t.GetCD()
    return logicMgr.NoticeManager.endTime - HelperFunc.GetServerTime()
end
function t.GetSubRankLimitCount(bigType,subType)
    if subType == 0 then return 50 end
    local cfg = dataTable.getDataTableByMultiId("t_noticebase",bigType,subType)
    if cfg then
        return cfg.f_Num
    end
    return 0
end
function t.GetSubRankList(bigType,subType,list)
    table.clear(list)
    for k,v in pairs(t.partRank.ranks) do
        table.insert(list,v)
    end
end
--是否祝福过
function t.IsBlessed()
    --return t.buffId > 0
    return true;--屏蔽祈福
end
function t.HasReward(rankType)
    local data = t.changeAwardList[rankType]
    if data and data.state == 0 then
        return true
    end
    return false
end
--有无异动
function t.HasAlertChange(rankType)
    local data = t.changeAwardList[rankType]
    if data then
        local cfg = dataTable.getDataTableById("t_noticeranklist",data.cfgId)
        if cfg and cfg.f_ChooseType ~= 5 then
            return true
        end
    end
    return false
end
--分榜排行
function t.GetMySelfSubRankLevel()
    if not t.partRank or not t.partRank.myRank then
        return 0
    end
    return t.partRank.myRank.rank
    --[[for k,v in pairs(t.partRank.ranks) do
        if t.partRank.myRank and t.partRank.myRank.playerId == v.playerId then
            return k
        end
    end
    return -1]]
end
function t.GetAbnormalChangeStr2(rankType,chooseType)
    local cfg = dataTable.getDataTable("t_noticeranklist")
    for k,v in pairs(cfg) do
        if v.f_RankType == rankType and v.f_ChooseType == chooseType then
            local tb =  v.f_Text
            local str
            if #tb == 1 then
                str = getLanguage(dataTable.getDataTableById("t_notice",tb[1],"f_Notice"))
            else
                local index = math.random(1,#tb)
                str = getLanguage(dataTable.getDataTableById("t_notice",tb[index],"f_Notice"))
            end
            return str
        end
    end
    return ""
end
--异动文字
function t.GetChangeStr(rankType,chooseType)
    local str = ""
    local cfg = dataTable.getDataTable("t_noticeranklist")
    for k,v in pairs(cfg) do
        if v.f_RankType == rankType and v.f_ChooseType == chooseType then
            local tb = decodeJsonStrToTabel(v.f_Text)
            if #tb == 1 then
                str = getLanguage(dataTable.getDataTableById("t_notice",tb[1],"f_Notice"))
            else
                local index = math.random(1,#tb)
                str = getLanguage(dataTable.getDataTableById("t_notice",tb[index],"f_Notice"))
            end
            local player = t.GetAlertPlayer(rankType)
            if player then
                str = string.format(str,player.name)
            end
            break
        end
    end
    return str
end
--异动文字
function t.GetAbnormalChangeStr(rankType)
    local str = ""
    for k,v in pairs(t.changeAwardList) do
        local cfg = dataTable.getDataTableById("t_noticeranklist",v.cfgId)
        if cfg and cfg.f_RankType == rankType then
            local tb = decodeJsonStrToTabel(cfg.f_Text)
            if #tb == 1 then
                str = getLanguage(dataTable.getDataTableById("t_notice",tb[1],"f_Notice"))
            else
                local index = math.random(1,#tb)
                str = getLanguage(dataTable.getDataTableById("t_notice",tb[index],"f_Notice"))
            end
            local player = t.GetAlertPlayer(rankType)
            if player then
                str = string.format(str,player.name)
            end
            return str
        end
    end
    return ""
end
function t.TryAddAlertNormalChange()
    local cfg = dataTable.getDataTable("t_noticeranklist")
    for k,v in pairs(cfg) do
        if v.f_ChooseType == 5 then
            if t.changeAwardList[v.f_RankType] == nil then
                t.changeAwardList[v.f_RankType] =  { cfgId = v.f_Id,state = 1}
            end
        end
    end
end
function t.AddAlertChange(db)
    local cfg = dataTable.getDataTableById("t_noticeranklist",db.cfgId)
    if cfg then
        if cfg.f_ChooseType == 5 then
            db.state = 1
        end
        logicMgr.NoticeManager.changeAwardList[cfg.f_RankType] = { cfgId = db.cfgId,state = db.state}
    else
        logError("找不到配置表 t_noticeranklist by id:"..db.cfgId)
    end
end
function t.GetAlertPlayer(rankType)
    return t.alertPlayerList[rankType]
end
function t.AddAlertPlayer(db)

    local player = {}
    player = t.CreatePlayerByDB(db.playerInfo,db.rank)
    t.alertPlayerList[db.rankType] = player
end
function t.CreatePlayerByDB(db,rank)
    if db == nil then return nil end

    local d = db.playerShowInfo
    if not d then
        logError("找不到playerShowInfo ")
        return
    end
    ---@class NoticePlayerInfo
    local player = {}
    player.playerId = tostring(d.playerId)
    player.name = d.name
    player.headId = d.pic;
    player.picFrame = d.picFrame
    player.background = d.background
    player.level = d.level
    --param
    player.formation = {}
    for k,v in pairs(db.formation) do
        table.insert(player.formation,tonumber(tostring(v)))
    end
    player.vip = db.vip
    player.online = db.online == 1 and true or false
    local param = tonumber(db.parameter) or 0
    player.param = param
    player.power = getNumString(d.maxPower)
    player.rank = rank or -1
    player.time = 10
    return player
end
function t.GetRewards(rankType,subType,rankLevel)
    if subType == t.PartSubType.all then -- todo 后面有时间改下
        local numList = t.maxNumList[rankType]
        for k,v in ipairs(numList) do
            if rankLevel <= v then
                if k > 1 then
                    rankLevel = rankLevel - numList[k-1]
                end
                subType = k
                break
            end
        end
    end
    local cfg = dataTable.getDataTableByMultiId("t_noticerank",rankType,subType,rankLevel)
    if not cfg then
        logError(string.format("找不到配置表t_noticerank：%d,%d,%d",rankType,subType,rankLevel))
        return nil
    end
        return logicMgr.ItemManager.ParseRewards(cfg.f_Rewards)
    --[[local cfg = dataTable.getDataTableByMultiId("t_noticerank",rankType)
    for k,v in pairs(cfg) do
        if rankLevel >= v.f_RankId1 and rankLevel <= v.f_RankId2 then
            if v.rewardsTB == nil then
                v.rewardsTB = logicMgr.ItemManager.ParseRewards(v.f_Rewards)
            end
            return v.rewardsTB
        end
    end]]
end
--分榜
function t.SetMyRank(rankData,rank,rankType,rankSubType)
    t.partRank.myRank = t.CreatePlayerByDB(rankData,rank)
    --t.bigRankType = rankType
    --t.subRankType = rankSubType
    t.curSubRankType = rankSubType
end
function t.SetDefaultSubRankType(bigRankType,subRankType)
    t.defaultSubRankTypeList[bigRankType] = subRankType
end
function t.GetDefaultSubRankType(bigRankType)
    return t.defaultSubRankTypeList[bigRankType] or 5
end
--分榜
function t.SetRanks(ranks)
    t.partRank.ranks = t.partRank.ranks or {}
    table.clear(t.partRank.ranks)
    for k,v in pairs(ranks) do
        local p = t.CreatePlayerByDB(v)
        table.insert(t.partRank.ranks,p)
    end
end

function t.SetPlayerGive(playerId)
    for i = 1, #t.PlayerRankList do
        if t.PlayerRankList[i].playerShowInfo.playerId==playerId then
            t.PlayerRankList[i].extend1= t.PlayerRankList[i].extend1+1;
            t.dayClick = t.dayClick + 1
            FireEvent(Config.EventType.FreshRankingLikeNum) 
            return;
        end
    end

end

function t.SetRankPlayList(msg)
    t.PlayerRankList=msg.playerList;
    for i = 1, #msg.playerList do
        -- body
        t.PlayerRankList[i]=msg.playerList[i];
        if i==1 then
                t.FirstRnaklist=msg.playerList[i];
        end
    end

    t.selfInfo  = msg.selfInfo
    t.myRank    = msg.myRank
    t.dayClick  = msg.dayClick
    t.sumClick  = msg.sumClick
    t.rank_balance_time = msg.firstEndTime and tonumber(tostring(msg.firstEndTime))/1000  or Config.serverTime/1000
    FireEvent(Config.EventType.FreshRankingLikeNum) 
end

--点赞总次数
function t.RefreshSumClick()
    t.sumClick = t.sumClick + 1
end

--自己奖励信息
---@param rewardDB FirstWeekRankAwardInfo
function t.SetMyRewards(rankType,rewardDB)
    if not rewardDB then return end
    local data = {}
    data.rank = rewardDB.rank
    data.received = rewardDB.received == 1 and true or false
    ---@type TempItemInfo
    data.items = rewardDB.award
    t.myReward[rankType] = data
end
---@param infoDBList OnSubRankRewardInfo[]
function t.SetSubRankRewardInfo(rankType,infoDBList)
    if not infoDBList then return end
    for k,v in pairs(infoDBList) do
        local index = t.subRankUnlockRewardList[v.rankType]
        if not index then
            t.subRankUnlockRewardList[v.rankType] = v.rankSubType
        else
            if index > v.rankSubType then
                t.subRankUnlockRewardList[v.rankType] = v.rankSubType
            end
        end
    end
end
function t.GetMyRankRewards(rankType)
    return t.myReward[rankType].items
end
--所有榜是否有奖励红点用
function t.HasRewardRedPointAll()
    for k,v in pairs(t.rewardRedPointList) do
        if t.HasRewardRedPoint(k) then
            return true
        end
    end
    return false
end
function t.HasRewardRedPoint(rankType)
    local flag = t.rewardRedPointList[rankType]
    return flag
end
--榜单是否有奖励
function t.HasRankReward(rankType)
    local data = t.myReward[rankType]
    if data == nil then return false end
    if data.received then return false end
    if data.items == nil or #data.items == 0 then return false end
    return true
end

--分榜是否解锁
function t.IsSubRankOpen(bigRank,subRank)
    local index = t.subRankUnlockRewardList[bigRank]
    if index == nil then
        return false
    end
    return subRank >= index
end

function t.SetMyRewardState(rankType,state)
    local data = t.myReward[rankType]
    if not data then return end
    data.received = state == 1 and true or false
end
--总榜
function t.SetMainRanks(msg)

    local ranks = t.mainRanks[msg.rankType]
    if ranks then
        ranks.myRank = nil
        table.clear(ranks.ranks)
    else
        ranks = {}
        ranks.ranks = {}
        t.mainRanks[msg.rankType] = ranks
    end
    ranks.myRank = t.CreatePlayerByDB(msg.selfInfo,-1)
    for k,v in pairs(msg.playerList) do
        local info = t.CreatePlayerByDB(v,k)
        if info and ranks.myRank.playerId == info.playerId then
            ranks.myRank.rank = k
        end
        table.insert(ranks.ranks,info)
    end

end
--曹丹的红点
function t.RedDotsDelayCheckReq()
    --[[if t.IsTimeEnd() then
        return
    end
    local time = HelperFunc.GetServerTime()
    local cron = dataTable.getGlobalString(118)
    local next = tonumber(logicMgr.CronMgr.GetNextDateTime(cron,time))
    t.CancelRedDotsDelayCheck()
    t.checkTimerId = utimer.delayCall(function()
        NoticeHandler.ReqRedDots(false,true)
        t.RedDotsDelayCheckReq()
    end,(next - time + 1) * 1000) --延一秒]]
end
function t.CancelRedDotsDelayCheck()
    utimer.remove(t.checkTimerId)
    t.checkTimerId = nil
end
function t.OnDestroy()
    t.CancelRedDotsDelayCheck()
    t.sevenRankDay = nil;
    t.sevenRankPlusDay = nil;
    if  t.updateHourTime ~= nil then
        utimer.remove(t.updateHourTime)
        t.updateHourTime = nil;
    end
end

function NoticeManager.LoadNotice()
    t.notice_list = {}
    Http.Get(Config.PushNoticeUrl, nil, function(success, str)
        if not success or string.IsNilOrEmpty(str) then
            return
        end
       
        local ls = decodeJsonStrToTabel(str)
        if ls then
            for _,v in pairs(ls) do
                table.insert(t.notice_list, v)
            end
        end

        table.sort(t.notice_list, function(a,b)
            return a.index > b.index
        end)
    end)
end

function NoticeManager.GetNoticeList(cb)
    if t.notice_list == nil then
        logicMgr.NoticeManager.LoadNotice()
    end
    return t.notice_list
end

--公告强弹
function NoticeManager.ConstraintShowNotice(callBack)
    t.notice_list = {}
    --  local url = "https://fszx-oss.oss-cn-shanghai.aliyuncs.com/notice/android/1/notice.txt"--测试地址
    --  Http.Get(url, nil, function(success, str)
    YanhuaMMO.Http.httpReqTimeOut = 8
    Http.Get(Config.PushNoticeUrl, nil, function(success, str)
        if not success or string.IsNilOrEmpty(str) then
            if callBack then
                callBack()
                log(">>>>>>>>>>>>>>公告获取失败直接登录")
            end
            return
        end
        local ls = decodeJsonStrToTabel(str)
        if ls then
            for _,v in pairs(ls) do
                table.insert(t.notice_list, v)
            end
        end

        table.sort(t.notice_list, function(a,b)
            if a.index ~= b.index then
                return a.index > b.index
            end
            return a.id > b.id
        end)

        local need_cons = false
        local notice = {}

        for _,v in pairs(t.notice_list) do
            if string.IsNilOrEmpty(v.version) then
                v.version = ""
            end
            if v.push == 1 and logicMgr.LoginManager~= nil and logicMgr.LoginManager.user_id ~= nil and not logicMgr.NoticeManager.GetPlayerPrefsTodayFlag(logicMgr.LoginManager.user_id .. "_PushNoticeTip_"..v.version)  then
                need_cons = true
                table.insert(notice, v)
            end
        end

        if need_cons then
            for k,v in pairs(notice) do
                if logicMgr.LoginManager~= nil and logicMgr.LoginManager.user_id ~= nil then
                    logicMgr.NoticeManager.SetPlayerPrefsTodayFlag(logicMgr.LoginManager.user_id .. "_PushNoticeTip_"..v.version, true)
                end
            end
            
            showUI(PanelNames.UIOperationNotice, {constranint = true, callback = callBack})
            log(">>>>>>>>>>>>>>公告需要强弹")
        else
            if callBack then
                callBack()
                log(">>>>>>>>>>>>>>公告无需强弹直接登录")
            end
        end
    end)
end

function NoticeManager.GetPlayerPrefsTodayFlag(key, serverTime)
	local value = Util.GetPlayerPrefs(tostring(key))
	-- if not serverTime then
	-- 	serverTime = HelperFunc.GetServerTime()
	-- end

	-- if serverTime == 0 then
	-- 	serverTime = os.time()
	-- end

	-- local month = tostring(os.date("%m", serverTime))
	-- local day = tostring(os.date("%d", serverTime))
	-- local v = month .. day
	if value then
		return true
	else
		return false
	end
end

function NoticeManager.SetPlayerPrefsTodayFlag(key, hasValue, serverTime)
	-- if not serverTime then
	-- 	serverTime = HelperFunc.GetServerTime()
	-- end

	-- if serverTime == 0 then
	-- 	serverTime = os.time()
	-- end

	-- local month = tostring(os.date("%m", serverTime))
	-- local day = tostring(os.date("%d", serverTime))
	-- local v = month .. day
	if hasValue then
        Util.SetPlayerPrefs(key, 1)
	else
        Util.SetPlayerPrefs(key, "")
		-- SetPlayerPrefs(key, "")
	end
	
end

function NoticeManager.ClickNotice(version)
    if G.serverData and G.serverData["roleInfo"] then
        local key = table.concat({"notice_", tostring(G.serverData["roleInfo"].show.playerId), "_", version})
        local record = GetPlayerPrefs(key) or 0

        if record == 0 then
            SetPlayerPrefs(key, 1)
        end
    end
end

function NoticeManager.CheckRedPoint(version)
    local record = 0
    if G.serverData and G.serverData["roleInfo"] then
        local key = table.concat({"notice_", tostring(G.serverData["roleInfo"].show.playerId), "_", version})
        record = GetPlayerPrefs(key) or 0
    else
        return false
    end
   
    return record == 0
end

--region 公告
function NoticeManager.CheckNotice(checkTodayTip)
    showUI(PanelNames.UIOperationNotice)
end
--endregion

-------------------------------七天排行榜-----开始------------------------------
--- 获取排行分榜配置
function NoticeManager.getSevenRankSubReward(type, id)
    if id ~= nil then
        return G.dataTable["t_raceranksublistaward"][tostring(id)];
    end
    if type == nil or t.sevenRankRound == nil or t.subType == nil then
        return G.dataTable["t_raceranksublistaward"] or {};
    end
    local tableList = {};
    for k, v in pairs(G.dataTable["t_raceranksublistaward"]) do
        if type == v.f_Type and t.sevenRankRound == v.f_RankType and t.bigTypeId == v.f_BranchType and t.smallTypeId == v.f_BranchTypeSmall then
            table.insert(tableList, v);
        end
    end
    return tableList;
end
--- 获取排行榜展示数量
function NoticeManager.getSevenRankNumberByType()
    if t.subType == nil then
        return 0;
    end
    for k, v in pairs(G.dataTable["t_racenumberpeople"]) do
        if v.f_TypeId == t.subType then
            --print("<color=#33FFFF>自己所在榜单 TypeId = "..v.f_TypeId.."  bigTypeId = "..v.f_BigTypeId.."  smallTypeId = "..v.f_SmallTypeId.."  (1->A1、2->A2、3->A3、4->A4、5->A5、6->B1~Bx、7->C1、8->D1)</color>");
            return tonumber(v.f_Condition);
        end
    end
    return 0;
end
--- 获取排行配置
function NoticeManager.getSevenRankTableDataByType(type)
    if t.sevenRankRound == nil then
        return G.dataTable["t_racerankconditon"] or {};
    end
    local tableList;
    if type == nil then
        tableList = {};
        for k, v in pairs(G.dataTable["t_racerankconditon"]) do
            if t.sevenRankRound == v.f_RankType then
                table.insert(tableList, v);
            end
        end
    end
    for k, v in pairs(G.dataTable["t_racerankconditon"]) do
        if v.f_RaceTypeId == type and t.sevenRankRound == v.f_RankType then
            return v;
        end
    end
    return tableList;
end
--- 获取当天排行榜类型
function NoticeManager.getSevenRankType()
    if t.sevenRankDay == nil or t.sevenRankRound == nil then
        return nil;
    end
    for k, v in pairs(G.dataTable["t_racerankconditon"]) do
        if v.f_Day == t.sevenRankDay and t.sevenRankRound == v.f_RankType then
            return v.f_RaceTypeId;
        end
    end
    return nil;
end
--- 获取排行奖励配置
function NoticeManager.getSevenRankRewardTableDataByType(type)
    if type == nil or t.sevenRankRound == nil then
        return G.dataTable["t_racerankaward"] or {};
    end
    local tableList = {};
    for k, v in pairs(G.dataTable["t_racerankaward"]) do
        if t.sevenRankRound == v.f_RankType and type == v.f_Type then
            table.insert(tableList, v);
        end
    end
    return tableList;
end
--- 获取排行里程碑奖励配置
function NoticeManager.getSevenRankConditionRewardTableDataByType(type, id)
    if id ~= nil then
        return G.dataTable["t_raceconditionaward"][tostring(id)];
    end
    if type == nil or t.sevenRankRound == nil then
        return G.dataTable["t_raceconditionaward"] or {};
    end
    local tableList = {};
    for k, v in pairs(G.dataTable["t_raceconditionaward"]) do
        if v.f_Type == type and t.sevenRankRound == v.f_RankType then
            table.insert(tableList, v);
        end
    end
    return tableList;
end
function NoticeManager.updateMySevenRankByType(type, score)
    --t._serverData = t.getSevenRankServerDataByType(type);
    --local _rank = -1;
    --local _myIndex = -1;
    --local _myLastRank = -1;
    --for k, v in pairs(t._serverData) do
    --    if v.score <= score and v.rank > _rank then
    --        _rank = v.rank;
    --    end
    --    if v.playerId == logicMgr.HeroManager.GetRoleId() then
    --        _myIndex = k;
    --        _myLastRank = v.rank;
    --        t._serverData[_myIndex].score = score;
    --    end
    --end
    --local _myRank = 0;
    --if _rank == -1 then
    --    if _myIndex ~= -1 then
    --        t._serverData[_myIndex].rank = 50;
    --        _myRank = 50;
    --    end
    --end
end
---设置后端排行榜数据
function NoticeManager.setSevenRankServerDataByType(type, rank, round)
    t.sevenRankRound = round;
    t.sevenRankData = t.sevenRankData or {};
    t.sevenRankData[type] = rank;
    for k, v in pairs(t.sevenRankData[type]) do
        v.rank = tonumber(tostring(v.rank));
        v.score = tonumber(tostring(v.score));
        v.playerId = tostring(v.playerId);
    end
end
---获取后端排行榜数据
function NoticeManager.getSevenRankServerDataByType(type)
    t.sevenRankData = t.sevenRankData or {};
    return t.sevenRankData[type] or {};
end

---设置自己所在榜单
function NoticeManager.setSubTypeServerData(bigType, smallType)
    t.bigTypeId = nil;
    t.smallTypeId = nil;
    t.subType = nil;
    for k, v in pairs(G.dataTable["t_racenumberpeople"]) do
        if v.f_BigTypeId == bigType and v.f_SmallTypeId == smallType then
            t.bigTypeId = bigType;
            t.smallTypeId = smallType;
            t.subType = v.f_TypeId;
            break;
        end
    end
end

---获取排行榜规则
function NoticeManager.getSevenRankRule(type)
    if type == nil then
        return nil;
    end
    for k, v in pairs(G.dataTable["t_racerankexplain"]) do
        if v.f_RaceTypeId == type then
            return v;
        end
    end
    return nil;
end

---设置后端分榜数据
function NoticeManager.setSubSevenRankServerDataByType(type, rank)
    t.subSevenRankData = t.subSevenRankData or {};
    t.subSevenRankData[type] = rank;
    for k, v in pairs(t.subSevenRankData[type]) do
        v.rank = tonumber(tostring(v.rank));
        v.score = tonumber(tostring(v.score));
        v.playerId = tostring(v.playerId);
    end
end

---获取后端分榜数据
function NoticeManager.getSubSevenRankServerDataByType(type)
    t.subSevenRankData = t.subSevenRankData or {};
    return t.subSevenRankData[type] or {};
end

---设置我的后端排行榜数据
function NoticeManager.setMySevenRankByType(type, rank, score)
    t.sevenRankMyData = t.sevenRankMyData or {};
    t.sevenRankMyData[type] = t.sevenRankMyData[type] or {};
    t.sevenRankMyData[type].rank = t.sevenRankMyData[type].rank or 0;
    t.sevenRankMyData[type].score = t.sevenRankMyData[type].score or 0;
    if rank then
        t.sevenRankMyData[type].rank = tonumber(tostring(rank));
    end
    if score then
        t.sevenRankMyData[type].score = tonumber(tostring(score));
    end
end
---获取我的后端排行榜数据
function NoticeManager.getMySevenRankByType(type)
    t.sevenRankMyData = t.sevenRankMyData or {};
    return t.sevenRankMyData[type] or {rank=0, score=0};
end

---更新里程碑奖励领取状态
function NoticeManager.updateSevenRankReceiveAward(id)
    t.sevenRankReceiveAwardList = t.sevenRankReceiveAwardList or {};
    table.insert(t.sevenRankReceiveAwardList, id);
end
---设置里程碑奖励领取状态
function NoticeManager.setSevenRankReceiveAward(rewards)
    t.sevenRankReceiveAwardList = t.sevenRankReceiveAwardList or {};
    t.sevenRankReceiveAwardList = rewards;
end
---获取里程碑奖励领取状态
function NoticeManager.getSevenRankReceiveAward()
    t.sevenRankReceiveAwardList = t.sevenRankReceiveAwardList or {};
    return t.sevenRankReceiveAwardList or {};
end
---更新里程碑奖励领取数据
function NoticeManager.updateMySevenRankReceiveLimit(id, amount)
    t.sevenRankReceiveLimitList = t.sevenRankReceiveLimitList or {};
    for k, v in pairs(t.sevenRankReceiveLimitList) do
        if v.id == id then
            v.amount = amount;
            return ;
        end
    end
    local data = {};
    data.id = id;
    data.amount = amount;
    table.insert(t.sevenRankReceiveLimitList, data);
end
---设置里程碑奖励领取数据
function NoticeManager.setMySevenRankReceiveLimit(limitAmount)
    t.sevenRankReceiveLimitList = t.sevenRankReceiveLimitList or {};
    t.sevenRankReceiveLimitList = limitAmount;
end
---获取里程碑奖励领取数据
function NoticeManager.getMySevenRankReceiveLimit()
    t.sevenRankReceiveLimitList = t.sevenRankReceiveLimitList or {};
    return t.sevenRankReceiveLimitList;
end
---更新进度数据
function NoticeManager.updateSevenRankScore(type, score)
    score = tonumber(tostring(score));
    t.sevenRankScoreList = t.sevenRankScoreList or {};
    for k, v in pairs(t.sevenRankScoreList) do
        if v.type == type then
            v.score = score
            return ;
        end
    end
    local data = {};
    data.type = type;
    data.score = score;
    table.insert(t.sevenRankScoreList, data);
    t.updateMySevenRankByType(type, score);
end
---设置进度数据
function NoticeManager.setSevenRankScore(data)
    t.sevenRankScoreList = t.sevenRankScoreList or {};
    t.sevenRankScoreList = data;
    for k, v in pairs(t.sevenRankScoreList) do
        v.score = tonumber(tostring(v.score));
    end
end
---获取进度数据
function NoticeManager.getSevenRankScoreByType(type)
    t.sevenRankScoreList = t.sevenRankScoreList or {};
    if type == nil then
        return t.sevenRankScoreList;
    end
    for k, v in pairs(t.sevenRankScoreList) do
        if v.type == type then
            return v.score;
        end
    end
    return 0;
end
---获取里程碑奖励数据
function NoticeManager.getMySevenRankReceiveDataByType(type, notData)
    t._awardList = t.getSevenRankReceiveAward();
    t._limitList = t.getMySevenRankReceiveLimit();
    t._tableData = t.getSevenRankConditionRewardTableDataByType(type);
    table.sort(t._tableData, function (a, b)
        return a.f_Condition < b.f_Condition;
    end);
    t._score = t.getSevenRankScoreByType(type);
    local dataList = {};
    local minIndex = nil;
    local flag = nil;
    for k, v in pairs(t._tableData) do
        t._amount = 0;
        t._reward = 0;
        for _, vv in pairs(t._limitList) do
            if vv.id == v.f_RaceAwardId then
                t._amount = vv.amount;
                break
            end
        end
        for _, vv in pairs(t._awardList) do
            if vv == v.f_RaceAwardId then
                t._reward = 2;
                break
            end
        end
        if t._reward == 0 then
            if t._amount >= v.f_GetNum then
                t._reward = 3;
            elseif v.f_Condition <= t._score then
                t._reward = 1;
                if minIndex == nil then
                    minIndex = k;
                end
            else
                if flag == nil then
                    flag = k;
                end
            end
        end
        if not notData then
            local data = {};
            data.id = v.f_RaceAwardId;
            data.amount = t._amount;
            data.reward = t._reward;
            table.insert(dataList, data)
        end
    end
    return dataList, minIndex, flag;
end

function NoticeManager.setSevenRankDay(day, round)
    t.sevenRankDay = day;
    t.sevenRankRound = round;
end
function NoticeManager.getSevenRankDay()
    --注释的，都是前朝、前前朝...代码，策划就爱搞事
    --if true then
    --    return 7, HelperFunc.GetServerTime() + 10000;
    --end
    if t.sevenRankDay == nil or t.sevenRankRound == nil then
        return nil;
    end
    local time = logicMgr.LoginManager.getServerStartTime()
    local openDay = dataTable.getGlobalInt(426);
    --local endDay = dataTable.getGlobalInt(427);
    local temp = os.date("*t", math.floor(time));
    --local toTime = os.time({year = temp.year, month = temp.month, day = temp.day, hour = 0, min = 0, sec = 0});
    --小时先写死，策划配置最好
    local roundHour = dataTable.getGlobalString(429);
    if roundHour ~= nil then
        roundHour = string.sub(roundHour, 5, 6);
    else
        roundHour = 22;
    end
    roundHour = tonumber(roundHour);
    local openTime = os.time({year = temp.year, month = temp.month, day = temp.day, hour = roundHour, min = 0, sec = 0});
    if temp.hour >= roundHour then
        openTime = openTime + 24 * 60 * 60;
    end
    openTime = openTime + (openDay - 2) * 24 * 60 * 60;
    --时间计算很绕，不懂找策划，张卫峰
    --local openTime = toTime + (openDay - 1) * 24 * 60 * 60;
    --local endTime = openTime + endDay * 24 * 60 * 60;
    local nowTime = HelperFunc.GetServerTime();
    --if nowTime >= openTime and nowTime < endTime then
    if nowTime >= openTime then
        temp = os.date("*t", math.floor(nowTime));
        --toTime = os.time({year = temp.year, month = temp.month, day = temp.day, hour = 0, min = 0, sec = 0});
        --toTime = toTime + 24 * 60 * 60;
        local toTime = os.time({year = temp.year, month = temp.month, day = temp.day, hour = roundHour, min = 0, sec = 0});
        if temp.hour >= roundHour then
            toTime = toTime + 24 * 60 * 60;
        end
        local day = math.ceil((nowTime - openTime) / (24*60*60));
        -- 7天一轮回，策划配置最好
        local roundDay = dataTable.getGlobalInt(433);
        if roundDay == nil then
            roundDay = 7;
        end
        local dayIndex = day % roundDay;
        local round = math.modf(day / roundDay);
        if dayIndex ~= 0 then
            round = round + 1;
        end
        if dayIndex == 0 then
            dayIndex = 7;
        end
        --if dayIndex ~= t.sevenRankDay then
        --    print("前端 dayIndex = "..dayIndex.."，后端 day = "..t.sevenRankDay.."不一致");
        --end
        --if round ~= t.sevenRankRound then
        --    print("前端 round = "..round.."，后端 round = "..t.sevenRankRound.."不一致");
        --end

        return t.sevenRankDay, toTime, t.sevenRankRound;
    end
    return nil;
end
function NoticeManager.checkSevenRankIsRanking(day)
    if day == nil then
        return false;
    end
    t._tableData = t.getSevenRankTableDataByType();
    for k, v in pairs(t._tableData) do
        if v.f_Day >= day then
            return true;
        end
    end
    return false;
end

--- 七日排行榜签到奖励状态
function NoticeManager.setSevenRankSigninState(signReward)
    t.sevenRankSigninState = signReward;
end

function NoticeManager.getSevenRankSigninState()
    return t.sevenRankSigninState or 1;
end

function NoticeManager.getStageRewardTime()
    if t.stageRewardTime == nil then
        t.stageRewardTime = {};
        local roundHour = dataTable.getGlobalString(441);
        if roundHour ~= nil then
            roundHour = string.sub(roundHour, 5, 6);
        else
            roundHour = 12;
        end
        table.insert(t.stageRewardTime, tonumber(roundHour));

        roundHour = dataTable.getGlobalString(442);
        if roundHour ~= nil then
            roundHour = string.sub(roundHour, 5, 6);
        else
            roundHour = 16;
        end
        table.insert(t.stageRewardTime, tonumber(roundHour));

        roundHour = dataTable.getGlobalString(443);
        if roundHour ~= nil then
            roundHour = string.sub(roundHour, 5, 6);
        else
            roundHour = 20;
        end
        table.insert(t.stageRewardTime, tonumber(roundHour));

        roundHour = dataTable.getGlobalString(429);
        if roundHour ~= nil then
            roundHour = string.sub(roundHour, 5, 6);
        else
            roundHour = 22;
        end
        table.insert(t.stageRewardTime, tonumber(roundHour));
    end
    return t.stageRewardTime;
end

--- 临时维护下签到奖励状态
function NoticeManager.TempSevenRankSigninServerData()
    local temp = os.date("*t", math.floor(HelperFunc.GetServerTime()));
    local serverHour = tonumber(temp.hour);
    local stageTime = NoticeManager.getStageRewardTime();
    local num = #stageTime - 1;
    for i = 1, num do
        if serverHour == stageTime[i] then
            t.sevenRankSigninState = 0;
            FireEvent(Config.EventType.Fresh_SevenRankReward);
            return;
        end
    end
end

function NoticeManager.reqSevenRankTimingUpdate()
    if  t.updateHourTime ~= nil then
        utimer.remove(t.updateHourTime)
        t.updateHourTime = nil;
    end
    local rankType = NoticeManager.getSevenRankType();
    if rankType == nil then
        return;
    end
    local myRankServerData = logicMgr.NoticeManager.getMySevenRankByType(rankType);
    if myRankServerData.rank > 0 then
        return;
    end
    t.updateHourTime = utimer.timeCall(function()
        utimer.remove(t.updateHourTime)
        t.updateHourTime = nil;
        NoticeHandler.ReqSevenRankList(rankType);
    end, 90 * 1000, 1);
end



--- 七日排行榜Plus  坐等Plus Max or Mini
--- 获取排行榜奖励配置
function NoticeManager.getSevenRankPlusSubReward(type, id)
    if id ~= nil then
        return G.dataTable["t_raceranksublistawardnew"][tostring(id)];
    end
    if type == nil then
        return G.dataTable["t_raceranksublistawardnew"] or {};
    end
    local tableList = {};
    for k, v in pairs(G.dataTable["t_raceranksublistawardnew"]) do
        if type == v.f_Type then
            table.insert(tableList, v);
        end
    end
    return tableList;
end
--- 获取排行配置
function NoticeManager.getSevenRankPlusTableDataByType(type)
    local tableList;
    if type == nil then
        tableList = {};
        for k, v in pairs(G.dataTable["t_racerankconditonnew"]) do
            table.insert(tableList, v);
        end
    end
    for k, v in pairs(G.dataTable["t_racerankconditonnew"]) do
        if v.f_RaceTypeId == type then
            return v;
        end
    end
    return tableList;
end
--- 获取当天排行榜类型
function NoticeManager.getSevenRankPlusType()
    if t.sevenRankPlusDay == nil then
        return nil;
    end
    for k, v in pairs(G.dataTable["t_racerankconditonnew"]) do
        if v.f_Day == t.sevenRankPlusDay then
            return v.f_RaceTypeId;
        end
    end
    return nil;
end
--- 获取当天排行榜名字
function NoticeManager.getSevenRankPlusName()
    if t.sevenRankPlusDay == nil then
        return "";
    end
    for k, v in pairs(G.dataTable["t_racerankconditonnew"]) do
        if v.f_Day == t.sevenRankPlusDay then
            return v.f_RankButtonTips;
        end
    end
    return "";
end
--- 获取排行目标奖励配置
function NoticeManager.getSevenRankPlusRewardTableDataByType(type)
    if type == nil then
        return G.dataTable["t_raceconditionawardnew"] or {};
    end
    local tableList = {};
    for k, v in pairs(G.dataTable["t_raceconditionawardnew"]) do
        if type == v.f_Type then
            table.insert(tableList, v);
        end
    end
    return tableList;
end
---获取排行榜规则
function NoticeManager.getSevenRankPlusRule(type)
    if type == nil then
        return nil;
    end
    for k, v in pairs(G.dataTable["t_racerankexplainnew"]) do
        if v.f_RaceTypeId == type then
            return v;
        end
    end
    return nil;
end

function NoticeManager.getSevenRankPlusDay()
    if t.sevenRankPlusDay == nil then
        return nil;
    end
    local time = logicMgr.LoginManager.getServerStartTime()
    local openDay = dataTable.getGlobalInt(445);
    --local endDay = dataTable.getGlobalInt(427);
    local endDay = 7;
    local temp = os.date("*t", math.floor(time));
    local roundHour = dataTable.getGlobalString(429);
    if roundHour ~= nil then
        roundHour = string.sub(roundHour, 5, 6);
    else
        roundHour = 22;
    end
    roundHour = tonumber(roundHour);
    local openTime = os.time({year = temp.year, month = temp.month, day = temp.day, hour = roundHour, min = 0, sec = 0});
    if temp.hour < roundHour then
        openTime = openTime - 24 * 60 * 60;
    end
    openTime = openTime + (openDay - 1) * 24 * 60 * 60;
    local endTime = openTime + endDay * 24 * 60 * 60;
    local nowTime = HelperFunc.GetServerTime();
    if nowTime >= openTime and nowTime < endTime then
        local toTime = 0;
        if t.sevenRankPlusDay == 1 then
            toTime = openTime + 2 * 24 * 60 * 60;
        else
            temp = os.date("*t", math.floor(nowTime));
            toTime = os.time({year = temp.year, month = temp.month, day = temp.day, hour = roundHour, min = 0, sec = 0});
            if temp.hour >= roundHour then
                toTime = toTime + 24 * 60 * 60;
            end
        end
        return t.sevenRankPlusDay, toTime;
    end
    return nil;
end

function NoticeManager.setSevenRankPlusDay(day)
    t.sevenRankPlusDay = day;
end

function NoticeManager.setSevenRankPlusStepRewardState(type, tid)
    t.sevenRankPlusReceiveAwardList = t.sevenRankPlusReceiveAwardList or {};
    table.insert(t.sevenRankPlusReceiveAwardList, tid);
end

---设置目标奖励领取状态
function NoticeManager.setSevenRankPlusReceiveAward(rewards)
    t.sevenRankPlusReceiveAwardList = t.sevenRankPlusReceiveAwardList or {};
    t.sevenRankPlusReceiveAwardList = rewards;
end
---获取目标奖励领取状态
function NoticeManager.getSevenRankPlusReceiveAward()
    t.sevenRankPlusReceiveAwardList = t.sevenRankPlusReceiveAwardList or {};
    return t.sevenRankPlusReceiveAwardList or {};
end

---设置进度数据
function NoticeManager.setSevenRankPlusScore(data)
    t.sevenRankPlusScoreList = t.sevenRankPlusScoreList or {};
    t.sevenRankPlusScoreList = data;
    for k, v in pairs(t.sevenRankPlusScoreList) do
        v.score = tonumber(tostring(v.score));
    end
end
---更新进度数据
function NoticeManager.updateSevenRankPlusScore(type, score)
    score = tonumber(tostring(score));
    t.sevenRankPlusScoreList = t.sevenRankPlusScoreList or {};
    for k, v in pairs(t.sevenRankPlusScoreList) do
        if v.type == type then
            v.score = score
            return;
        end
    end
    local data = {};
    data.type = type;
    data.score = score;
    table.insert(t.sevenRankPlusScoreList, data);
end
---获取进度数据
function NoticeManager.getSevenRankPlusScoreByType(type)
    t.sevenRankPlusScoreList = t.sevenRankPlusScoreList or {};
    if type == nil then
        return t.sevenRankPlusScoreList;
    end
    for k, v in pairs(t.sevenRankPlusScoreList) do
        if v.type == type then
            return v.score;
        end
    end
    return 0;
end

---设置我的后端排行榜数据
function NoticeManager.setMySevenRankPlusByType(type, rank, score)
    t.sevenRankPlusMyData = t.sevenRankPlusMyData or {};
    t.sevenRankPlusMyData[type] = t.sevenRankPlusMyData[type] or {};
    t.sevenRankPlusMyData[type].rank = t.sevenRankPlusMyData[type].rank or 0;
    t.sevenRankPlusMyData[type].score = t.sevenRankPlusMyData[type].score or 0;
    if rank then
        t.sevenRankPlusMyData[type].rank = tonumber(tostring(rank));
    end
    if score then
        t.sevenRankPlusMyData[type].score = tonumber(tostring(score));
    end
end

---获取我的后端排行榜数据
function NoticeManager.getMySevenRankPlusByType(type)
    t.sevenRankPlusMyData = t.sevenRankPlusMyData or {};
    return t.sevenRankPlusMyData[type] or {rank=0, score=0};
end

---设置后端排行榜数据
function NoticeManager.setSevenRankPlusServerDataByType(type, rank)
    t.sevenRankPlusData = t.sevenRankPlusData or {};
    t.sevenRankPlusData[type] = rank;
    for k, v in pairs(t.sevenRankPlusData[type]) do
        v.rank = tonumber(tostring(v.rank));
        v.score = tonumber(tostring(v.score));
        v.playerId = tostring(v.playerId);
    end
end

---获取后端排行榜数据
function NoticeManager.getSevenRankPlusServerDataByType(type)
    t.sevenRankPlusData = t.sevenRankPlusData or {};
    return t.sevenRankPlusData[type] or {};
end

---获取阶段奖励数据
function NoticeManager.getMySevenRankPlusReceiveDataByType(type)
    local curType = type;
    if curType == nil then
        curType = NoticeManager.getSevenRankPlusType();
    end
    if curType == nil then
        return false;
    end
    local tableData = NoticeManager.getSevenRankPlusRewardTableDataByType(curType);
    table.sort(tableData, function (a, b)
        return a.f_Condition < b.f_Condition;
    end);
    local score = t.getSevenRankPlusScoreByType(curType);
    local awardList = NoticeManager.getSevenRankPlusReceiveAward();
    local hasReward = false;
    for k, v in pairs(tableData) do
        if score >= tonumber(v.f_Condition) then
            hasReward = true;
            for _, vv in pairs(awardList) do
                if vv == v.f_RaceAwardId then
                    hasReward = false;
                    break;
                end
            end
        end
    end
    return hasReward;
end

---设置礼包红点
function NoticeManager.setMySevenRankPlusShopRedDots()
    SetPlayerPrefsTodayFlag("SevenRankPlusShopRedDots", true);
end
---获取礼包红点
function NoticeManager.getMySevenRankPlusShopRedDots()
    return not GetPlayerPrefsTodayFlag("SevenRankPlusShopRedDots");
end
--- Plus End

-------------------------------七天排行榜-----结束------------------------------
return NoticeManager